<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="dzyf" label="对账月份">
          <a-date-picker v-model:value="formState.dzyf" picker="month" value-format="YYYY-MM" @change="getData" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex justify-between items-center">
        <div class="text-xl w-1/12 text-center">{{ getSczt(tableData?.dzzb) }}</div>
        <div class="mt-3 w-11/12">
          <a-descriptions class="" :column="6">
            <a-descriptions-item label="医药机构编号"> {{ tableData?.dzzb?.yyjgbh || '-' }}</a-descriptions-item>
            <a-descriptions-item label="医疗机构填报部门">{{ tableData?.dzzb?.yljgtbbm || '-' }}</a-descriptions-item>
            <a-descriptions-item label="医疗机构填报人"> {{ tableData?.dzzb?.yljgtbr || '-' }}</a-descriptions-item>
            <a-descriptions-item label="对账地点类"> {{ tableData?.dzzb?.dzddlb === '0' ? '药材' : '物资' }}</a-descriptions-item>
            <a-descriptions-item label="医药机构医保区划">{{ tableData?.dzzb?.yyjgybqh || '-' }}</a-descriptions-item>
          </a-descriptions>
          <a-descriptions class="" :column="6">
            <a-descriptions-item label="医药机构名称"> {{ tableData?.dzzb?.yyjgmc || '-' }}</a-descriptions-item>
            <a-descriptions-item label="医疗机构填报人ID">{{ tableData?.dzzb?.yljgtbrid || '-' }}</a-descriptions-item>
            <a-descriptions-item label="医疗机构填报时间"> {{ tableData?.dzzb?.yljgtbsj || '-' }}</a-descriptions-item>
            <a-descriptions-item label="上传批次"> {{ tableData?.dzzb?.scpc === '0' ? '首次' : '非首次' }}</a-descriptions-item>
            <a-descriptions-item label="对账备注信息">{{ tableData?.dzzb?.dzbzxx || '-' }}</a-descriptions-item>
            <a-descriptions-item label="上传批次号"> {{ tableData?.dzzb?.jssbScpch || '-' }}</a-descriptions-item>
          </a-descriptions>
        </div>
      </div>
    </a-card>
    <a-card class="mt-2">
      <div class="flex justify-end">
        <BaseButton danger type="primary" confirm-text="确认删除?" :disabled="!tableData?.dzzb" @click="onDelete()"> 删除 </BaseButton>
        <a-button class="mb-2 ml-2" :disabled="!tableData?.dzzb" type="primary" @click="onClickOperate"> 对账数据清理 </a-button>

        <a-button class="mb-2 ml-2" type="primary" :disabled="!tableData?.dzzb" @click="onClickUpload"> 上传固化 </a-button>
        <a-button class="mb-2 ml-2" type="primary" :disabled="!tableData?.dzzb" @click="onClickReconciliation"> 确认对账 </a-button>
      </div>
      <BaseTable
        fill
        :data-source="tableData?.dzmxbList"
        :loading="loading"
        row-key="id"
        :pagination="{ total: page.total }"
        :exclude-columns="['jsrq']"
        column-code="000795"
        :scroll="{ x: 2200 }"
      >
        <template #bodyCell="{ column, index }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'

const formRef = ref(null)
const formState = ref({
  dzyf: dayjs().subtract(1, 'month').format('YYYY-MM')
})
const btnConfirm = ref(true)
const loading = ref(false)
const tableData = ref([])
const page = ref({
  total: 0
})

const getData = async () => {
  loading.value = true
  const { code, data, msg, total } = await post('/insureReconciliation/getGbdzYdzzInfo', formState.value)
  loading.value = false
  if (code !== 0) return message.error(msg || '查询失败')
  tableData.value = data
  if (tableData.value?.dzzb.sczt === '1') {
    btnConfirm.value = false
  } else {
    btnConfirm.value = true
  }
  page.value.total = total
}
const getSczt = (data) => {
  if (data?.dzzt === '2') {
    return '对账失败'
  } else if (data?.dzzt === '1') {
    return '对账成功'
  } else if (data?.sczt === '0') {
    return '未上传'
  } else if (data?.sczt === '1') {
    return '已上传'
  } else {
    return '未上传'
  }
}
const onClickOperate = async () => {
  const { code, msg } = await post(
    '/insureReconciliation/doWithdrawYdz',
    {
      ...tableData.value?.dzzb
    },
    { loading: true }
  )
  if (code !== 0) return message.error(msg || '操作失败')
  message.success(msg || '操作成功')
  getData()
}
const onClickUpload = async () => {
  const { code, msg } = await post(
    '/insureReconciliation/doUploadSolidYdz',
    {
      ...tableData.value?.dzzb
    },
    { loading: true }
  )
  if (code !== 0) return message.error(msg || '操作失败')
  message.success(msg || '操作成功')

  getData()
}

const onClickReconciliation = async () => {
  const { code, msg } = await post(
    '/insureReconciliation/doConfirmDzYdz',
    {
      ...tableData.value?.dzzb
    },
    { loading: true }
  )
  if (code !== 0) return message.error(msg || '操作失败')
  message.success(msg || '操作成功')
  getData()
}

const onDelete = async () => {
  const { code, msg } = await post(
    '/insureReconciliation/doDeleteYdz',
    {
      ...tableData.value?.dzzb
    },
    { loading: true }
  )
  if (code !== 0) return message.error(msg || '操作失败')
  message.success(msg || '操作成功')
  getData()
}
</script>
<style></style>
